package com.ldj.struts.action;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.ldj.common.MenuBuilder;
import com.ldj.common.SystemConfig;
import com.ldj.common.SystemSession;
import com.ldj.hibernate.LawInfo;
import com.ldj.hibernate.LawInfoDAO;
import com.ldj.hibernate.PersionFile;
import com.ldj.hibernate.PersionFileDAO;
import com.ldj.hibernate.SysLogInfo;
import com.ldj.hibernate.SysLogInfoDAO;
import com.ldj.struts.form.LawInfoForm;

public class LawInfoAction extends DispatchAction {

	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
			LawInfoForm lForm = (LawInfoForm)form;
			LawInfoDAO dao = new LawInfoDAO(request, response);
			String startDate = request.getParameter("newsInfoStartDate");
			String endDate = request.getParameter("newsInfoEndDate");
			List list = dao.findAllPage(startDate,endDate,lForm);
			request.setAttribute("list", list);
			request.setAttribute("intPage", String.valueOf(dao.intPage));
			return mapping.findForward("list");
	}
	
	public ActionForward toAdd(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
			return mapping.findForward("toAdd");
	}
	
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		LawInfoForm lForm = (LawInfoForm)form;
		LawInfo info = new LawInfo();
		info.setLawInfoTitle(lForm.getLawInfoTitle());
		info.setLawInfoType(lForm.getLawInfoType());
		info.setLawInfoContent(lForm.getLawInfoContent());
		info.setLawInfoPoint(0L);
		info.setLawInfoTime(new Date());
		PersionFileDAO persionFiledao = new PersionFileDAO(request, response);
		PersionFile persionInfo = persionFiledao.findById(SystemSession.getSupUserId(request));
		info.setPersionFile(persionInfo);
		info.setLawInfoScope("");
		info.setLawInfoState("0");
		LawInfoDAO lawInfoDao = new LawInfoDAO(request, response);
		lawInfoDao.save(info);
		SysLogInfoDAO logDao = new SysLogInfoDAO(null, null);
		SysLogInfo s = new SysLogInfo();
		s.setPersionFile(SystemSession.getSupUserInfo(request));
		s.setSysLogInfoOperDate(new Date());
		s.setSysLogInfoSupOrganCode(SystemSession.getSupUserInfo(request).getPerFileOrganCode());
		s.setSysLogInfoOperType("0");
		s.setSysLogInfoOperRecord(SystemConfig.getString("syslog", "linfo.add").replace("$user", SystemSession.getSupUserInfo(request).getPerFileName()).replace("$record", info.getLawInfoTitle()));
		logDao.save(s);
		return new ActionForward("/LawInfo.do?method=list", true);
	}
	
	public ActionForward detail(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		LawInfoForm lForm = (LawInfoForm)form;
		LawInfoDAO lawInfoDao = new LawInfoDAO(request, response);
		LawInfo linfo = lawInfoDao.findById(lForm.getLawInfoId());
		request.setAttribute("linfo", linfo);
		return mapping.findForward("detail");
	}

	public ActionForward toUpdate(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {

		LawInfoForm lForm = (LawInfoForm)form;
		LawInfoDAO dao = new LawInfoDAO(request, response);
		LawInfo info = dao.findById(lForm.getLawInfoId());
		request.setAttribute("nowPage",request.getParameter("page"));
		request.setAttribute("lInfo", info);		
		return mapping.findForward("toUpdate");
	
	}
	
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		LawInfoForm lForm = (LawInfoForm)form;
		LawInfoDAO dao = new LawInfoDAO(request, response);
		LawInfo info = dao.findById(lForm.getLawInfoId());
		info.setLawInfoContent(lForm.getLawInfoContent());
		info.setLawInfoTitle(lForm.getLawInfoTitle());
		info.setLawInfoType(lForm.getLawInfoType());
		dao.merge(info);
		
		SysLogInfoDAO logDao = new SysLogInfoDAO(null, null);
		SysLogInfo s = new SysLogInfo();
		s.setPersionFile(SystemSession.getSupUserInfo(request));
		s.setSysLogInfoOperDate(new Date());
		s.setSysLogInfoOperType("1");
		s.setSysLogInfoSupOrganCode(SystemSession.getSupUserInfo(request).getPerFileOrganCode());
		s.setSysLogInfoOperRecord(SystemConfig.getString("syslog", "LawInfo.update").replace("$user", SystemSession.getSupUserInfo(request).getPerFileName()).replace("$record", info.getLawInfoTitle()));
		logDao.save(s);
		return new ActionForward("/LawInfo.do?method=list&pages="+request.getParameter("pages"), true);
	}
	
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		LawInfoForm lForm = (LawInfoForm)form;
		LawInfoDAO lawInfoDao = new LawInfoDAO(request, response);
		LawInfo linfo = lawInfoDao.findById(lForm.getLawInfoId());
		lawInfoDao.delete(linfo);
		SysLogInfoDAO logDao = new SysLogInfoDAO(null, null);
		SysLogInfo s = new SysLogInfo();
		s.setPersionFile(SystemSession.getSupUserInfo(request));
		s.setSysLogInfoOperDate(new Date());
		s.setSysLogInfoOperType("2");
		s.setSysLogInfoSupOrganCode(SystemSession.getSupUserInfo(request).getPerFileOrganCode());
		s.setSysLogInfoOperRecord(SystemConfig.getString("syslog", "LawInfo.delete").replace("$user", SystemSession.getSupUserInfo(request).getPerFileName()).replace("$record",linfo.getLawInfoTitle()));
		logDao.save(s);
		return new ActionForward("/LawInfo.do?method=list&pages="+request.getParameter("page"), true);
	}

	public ActionForward lawPlaceTree(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		LawInfoDAO dao = new LawInfoDAO(request,response);
		String lawInfoId = request.getParameter("lawInfoId");
		LawInfo linfo=dao.findById(lawInfoId);
		String lawInfoTitle = linfo.getLawInfoTitle();
		String value  = linfo.getLawInfoScope();
		if(value == null)
			value = "";
		MenuBuilder bu = new MenuBuilder();
		request.setAttribute("lawInfoTitle", lawInfoTitle);
		request.setAttribute("tree", bu.getAreaTree(request, response, "地区", "", "", value));		
		return mapping.findForward("lawPlaceTree");
	}
	public ActionForward addArea(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		LawInfoDAO dao = new LawInfoDAO(request,response);
		LawInfo lInfo = dao.findById(request.getParameter("lid"));
		String[] code= request.getParameterValues("functionMenuCode");
		String codeString="";
		if(code != null && code.length > 0)
			for(int i=0;i<code.length;i++){
				 codeString+=code[i] + ",";
			}
		
		lInfo.setLawInfoScope(","+codeString);
		lInfo.setLawInfoState("1");
		dao.merge(lInfo);
		
		SysLogInfoDAO logDao = new SysLogInfoDAO(null, null);
		SysLogInfo s = new SysLogInfo();
		s.setPersionFile(SystemSession.getSupUserInfo(request));
		s.setSysLogInfoOperDate(new Date());
		s.setSysLogInfoOperType("1");
		s.setSysLogInfoSupOrganCode(SystemSession.getSupUserInfo(request).getPerFileOrganCode());
		s.setSysLogInfoOperRecord(SystemConfig.getString("syslog", "linfo.publish").replace("$user", SystemSession.getSupUserInfo(request).getPerFileName()).replace("$record", lInfo.getLawInfoTitle()));
		logDao.save(s);
		return new ActionForward("/LawInfo.do?method=list", true);
		
	}
	
}